package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.blankj.utilcode.util.KeyboardUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.databinding.ActEditNickNameBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 修改昵称
 * ================================================
 *
 * @author gosha
 * @date 2023/10/25-13:59
 */
class EditNickNameActivity : BaseActivity() {
    lateinit var binding: ActEditNickNameBinding

    private var mTextWatcher: TextWatcher? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActEditNickNameBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "修改昵称"
        binding.titleBar.tvRightOption.visibility = View.GONE
    }

    //修改昵称
    private fun editNickName() {
        clearBodyParam()
        initBodyParam()
        bodyParams["nickname"] = binding.etNickName.text.toString()
        RetrofitManager.instance.editUserInfo(
            bodyParams,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this, d)
                }

                override fun specificHandle(responseBody: String) {
                    ToastUtils.showShort(this@EditNickNameActivity, "修改昵称成功")
                    finish()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@EditNickNameActivity, it)
                    }
                }
            })
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        KeyboardUtils.registerSoftInputChangedListener(this) {
            if (it == 0) {
                binding.etNickName.clearFocus()
            }
        }

        mTextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (s.toString().isNotEmpty()) {
                    binding.tvBtnConfirm.setBackgroundResource(R.drawable.shape_half_ff664fc3)
                } else {
                    binding.tvBtnConfirm.setBackgroundResource(R.drawable.shape_half_ffc8c8c8)
                }
            }
        }

        binding.etNickName.addTextChangedListener(mTextWatcher)

        binding.tvBtnConfirm.clickWithTrigger {
            if (binding.etNickName.text.toString().isEmpty()) {
                ToastUtils.showShort(this@EditNickNameActivity, "请输入要修改的昵称")
                return@clickWithTrigger
            }

            editNickName()
        }
    }

    override fun onDestroy() {
        binding.etNickName.removeTextChangedListener(mTextWatcher)
        removeDisposables(this)
        super.onDestroy()
    }
}